package ZooManagement;

import java.util.ArrayList;
import java.util.HashMap;

public class ZooManager {
    private HashMap<String, Zoo> zoos;

    public ZooManager() {
        this.zoos = new HashMap<>();
    }

    // API Methods
    public void addAnimal(String zooName, Animal animal) {
        if (zoos.containsKey(zooName)) {
            zoos.get(zooName).addAnimal(animal);
        } else {
            throw new IllegalArgumentException("Zoo " + zooName + " does not exist.");
        }
    }

    public boolean moveAnimal(String fromZoo, String toZoo, String animalName) {
        if (!zoos.containsKey(fromZoo) || !zoos.containsKey(toZoo)) {
            return false;
        }

        Zoo sourceZoo = zoos.get(fromZoo);
        Zoo destinationZoo = zoos.get(toZoo);
        Animal animal = sourceZoo.findAnimal(animalName);

        if (animal == null) {
            return false;
        }

        if (sourceZoo.removeAnimal(animalName)) {
            destinationZoo.addAnimal(animal);
            return true;
        }

        return false;
    }

    public ArrayList<Animal> listAnimals(String zooName) {
        if (zoos.containsKey(zooName)) {
            return zoos.get(zooName).getAnimals();
        }
        return new ArrayList<>(); // Return empty list if zoo doesn't exist
    }

    public double getTransportCost(String fromZoo, String toZoo) {
        if (!zoos.containsKey(fromZoo) || !zoos.containsKey(toZoo)) {
            return -1.0; // Indicate error with negative value
        }

        // For simplicity, we'll calculate cost for the first animal in the zoo
        // In a real system, you'd specify which animal to transport
        Zoo sourceZoo = zoos.get(fromZoo);
        if (sourceZoo.getAnimals().isEmpty()) {
            return 0.0;
        }

        Animal sampleAnimal = sourceZoo.getAnimals().get(0);
        return Logistics.calculateTransportCost(sourceZoo, zoos.get(toZoo), sampleAnimal);
    }

    // Additional helper methods for managing zoos
    public void addZoo(Zoo zoo) {
        zoos.put(zoo.getName(), zoo);
    }

    public boolean removeZoo(String zooName) {
        return zoos.remove(zooName) != null;
    }

    public Zoo getZoo(String zooName) {
        return zoos.get(zooName);
    }

    public ArrayList<String> listZooNames() {
        return new ArrayList<>(zoos.keySet());
    }
}